package com.pwc.conversionTool.csv;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import javax.naming.InitialContext;

import au.com.bytecode.opencsv.CSVReader;

public class CsvHelper {
	
	private static File csvFile;
	private static List<String[]> list;


	
	public CsvHelper(String path) throws IOException
	{
		csvFile=new File(path);		
		initialCsvHelper( csvFile);
	}
	
	public CsvHelper(File file) throws IOException
	{
		 initialCsvHelper( file);
	}
	
	/**
	 * initial
	 * @param file
	 * @throws IOException
	 */
	private void initialCsvHelper(File file) throws IOException
	{
		csvFile=file;
		CSVReader csvreader = new CSVReader(new FileReader(csvFile.getPath()));
		list = csvreader.readAll();
		csvreader.close();
	}
	
	
	
	public static String[] getHeader()
	{
		return  list.get(0);
	}
	
	public static List<String[]> getContent()
	{
		if(list.size()>1)
		{
			List<String[]> returnList=new ArrayList<String[]>(list);
			 returnList .remove(0);
			 return returnList;
		}else {
			return null;
		}
	}
	
	/**
	 * 
	 * @return
	 */
	public static  String getKey()
	{
		String[] headerStrings=getHeader();
		StringBuilder stringBuilder=new StringBuilder();
		for (String item : headerStrings) {
			stringBuilder.append(item).append("|");			
		}
		
		stringBuilder.deleteCharAt(stringBuilder.length()-1);
		
		return stringBuilder.toString();
		
	}
	
	
	public static List<String[]> getAll()
	{
		return list;
	}

	public static File getCsvFile() {
		return csvFile;
	}

	public static void setCsvFile(File csvFile) {
		CsvHelper.csvFile = csvFile;
	}
	

}
